﻿Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices

Public Class ResetAdpwd
    Public Shared Sub Unlock(struser)
        Using ctx As New PrincipalContext(ContextType.Domain)
            Using p = Principal.FindByIdentity(ctx, struser)
                If Not p Is Nothing Then
                    Dim objuser As New DirectoryEntry("LDAP://" & p.DistinguishedName)
                    Dim value As Integer = CInt(Int((99 * Rnd()) + 1))
                    Dim strpassword As String = "B@tm@n" & value
                    objuser.Invoke("SetPassword", strpassword)
                    objuser.CommitChanges()
                    MessageBox.Show("Password for " & struser & " has been reset to: " & strpassword & "")
                End If
            End Using
        End Using
    End Sub
End Class
